package com.liuh;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.druid.support.http.util.IPAddress;
import com.alibaba.druid.support.http.util.IPRange;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.IPlugin;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.IDataSourceProvider;
import com.jfinal.plugin.druid.DruidStatViewHandler;
import com.jfinal.plugin.druid.IDruidStatViewAuth;
import com.liuh.fram.interceptor.ExceptionInterceptor;
import com.liuh.fram.log.Log4jLogFactoryExtra;
import com.liuh.fram.plug.QuartzPlugin;
import com.liuh.fram.util.SqlKit;
import com.liuh.web.BillInfoM;
import com.liuh.web.HelloController;
import com.liuh.web.LoginC;

public class BaseConfig extends JFinalConfig {

	@Override
	public void configConstant(Constants me) {
		// TODO Auto-generated method stub
		PropKit.use("conf_dev.properties");
		// 设置log处理器、开发模式 
		me.setLogFactory(new Log4jLogFactoryExtra());
		me.setDevMode(PropKit.getBoolean("devMode"));
	}

	@Override
	public void configRoute(Routes me) {
		// TODO Auto-generated method stub
		me.add("/", LoginC.class);
		me.add("/hello", HelloController.class);
	}

	@Override
	public void configPlugin(Plugins me) {
		Object obj = initDataSource("druid");
		me.add((IPlugin) obj);
		ActiveRecordPlugin arp = new ActiveRecordPlugin(
				(IDataSourceProvider) obj);
		//arp.setShowSql(PropKit.getBoolean("devMode"));
		//
		arp.addMapping("bill_info", "bi_code", BillInfoM.class);
		me.add(arp);
		// 定时任务
		QuartzPlugin quartzPlugin = new QuartzPlugin("quartzjob.properties");
		me.add(quartzPlugin);
		//quartzPlugin.start();
		// 缓存
		//me.add(new EhCachePlugin());
	}

	@Override
	public void configInterceptor(Interceptors me) {
		// TODO Auto-generated method stub
		ExceptionInterceptor interceptor = new ExceptionInterceptor();
		SessionInViewInterceptor sessionInViewInterceptor = new SessionInViewInterceptor();
		me.add(interceptor);
		me.add(sessionInViewInterceptor);
	}

	@Override
	public void configHandler(Handlers me) {
		me.add(new ContextPathHandler());
		me.add(new DruidStatViewHandler("/druid", new IDruidStatViewAuth() {
			public boolean isPermitted(HttpServletRequest request) {
				// return true;
				// 是否有权限访问druid
				return isPermittedRequest(request);
			}
		}));
	}

	/**
	 * 建议使用 JFinal 手册推荐的方式启动项目 运行此 main
	 * 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
	 */
	public static void main(String[] args) {
		JFinal.start("WebContent", 89, "/", 5);
	}
	
	/**
	 * @note 初始化数据源
	 * @author liuh
	 * */
	private Object initDataSource(String flag) {
		String url = PropKit.get("jdbcUrl");
		String user = PropKit.get("user");
		String pwd = PropKit.get("password");
		if (flag.equalsIgnoreCase("c3p0")) {
			return new SqlKit().initC3p0(url, user, pwd);
		} else if (flag.equalsIgnoreCase("druid")) {
			return new SqlKit().initDruid(url, user, pwd);
		}
		return null;
	}

	/**
	 * @note ip是否有权限查看
	 * @author liuh
	 * */
	private boolean isPermittedRequest(HttpServletRequest request) {
		String remoteAddress = request.getRemoteAddr();
		IPAddress ipAddress = new IPAddress(remoteAddress);
		List<IPRange> allowList = loadAllowIp(PropKit.get("allowIp"));
		if (allowList.size() > 0) {
			for (IPRange range : allowList) {
				if (range.isIPAddressInRange(ipAddress)) {
					return true;
				}
			}
			return false;
		}
		return true;
	}

	/**
	 * @note 允许查看管理端的ip地址
	 * @author liuh
	 * */
	private List<IPRange> loadAllowIp(String allowIps) {
		List<IPRange> allowList = new ArrayList<IPRange>();
		String[] items = allowIps.split(",");
		for (String item : items) {
			if ((item != null) && (item.length() != 0)) {
				IPRange ipRange = new IPRange(item);
				allowList.add(ipRange);
			}
		}
		return allowList;
	}

}
